package com.javaxiaobear.module.monitor.mapper;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import com.javaxiaobear.base.common.utils.PageUtils;
import com.javaxiaobear.module.monitor.domain.SysOperLog;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.constant.SqlOperator;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.SqlOperators;
import java.util.List;
import org.apache.ibatis.annotations.Delete;

/**
 * 操作日志 数据层
 *
 * @author javaxiaobear
 */
public interface SysOperLogMapper extends BaseMapper<SysOperLog> {
  /**
   * 新增操作日志
   *
   * @param operLog 操作日志对象
   */
  default void insertOperlog(SysOperLog operLog) {
    insert(operLog);
  }

  /**
   * 查询系统操作日志集合
   *
   * @param operLog 操作日志对象
   * @return 操作日志集合
   */
  default List<SysOperLog> selectOperLogList(SysOperLog operLog) {
    return selectListByQuery(
        QueryWrapper.create(
                operLog,
                SqlOperators.of(SysOperLog::getOperIp, SqlOperator.LIKE)
                    .of(SysOperLog::getTitle, SqlOperator.LIKE)
                    .of(SysOperLog::getOperName, SqlOperator.LIKE))
            .in(
                SysOperLog::getBusinessType,
                operLog.getBusinessTypes(),
                ArrayUtil.isNotEmpty(operLog.getBusinessTypes()))
            .ge(
                SysOperLog::getOperTime,
                operLog.getParams().get("beginTime"),
                operLog.getParams().get("beginTime") != null)
            .le(
                SysOperLog::getOperTime,
                operLog.getParams().get("endTime"),
                operLog.getParams().get("endTime") != null)
            .orderBy(SysOperLog::getOperIp, false));
  }

  default Page<SysOperLog> selectOperLogPage(SysOperLog operLog) {
    return paginate(
        PageUtils.getPage(),
        QueryWrapper.create(
                operLog,
                SqlOperators.of(SysOperLog::getOperIp, SqlOperator.LIKE)
                    .of(SysOperLog::getTitle, SqlOperator.LIKE)
                    .of(SysOperLog::getOperName, SqlOperator.LIKE))
            .in(
                SysOperLog::getBusinessType,
                operLog.getBusinessTypes(),
                ArrayUtil.isNotEmpty(operLog.getBusinessTypes()))
            .ge(
                SysOperLog::getOperTime,
                operLog.getParams().get("beginTime"),
                operLog.getParams().get("beginTime") != null)
            .le(
                SysOperLog::getOperTime,
                operLog.getParams().get("endTime"),
                operLog.getParams().get("endTime") != null)
            .orderBy(SysOperLog::getOperIp, false));
  }

  /**
   * 批量删除系统操作日志
   *
   * @param operIds 需要删除的操作日志ID
   * @return 结果
   */
  default int deleteOperLogByIds(Long[] operIds) {
    return deleteBatchByIds(ListUtil.of(operIds));
  }

  /**
   * 查询操作日志详细
   *
   * @param operId 操作ID
   * @return 操作日志对象
   */
  default SysOperLog selectOperLogById(Long operId) {
    return selectOneById(operId);
  }

  /** 清空操作日志 */
  @Delete("truncate table sys_oper_log")
  void cleanOperLog();
}
